﻿using System;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Collections.Generic;

namespace SilverlightGolf.Core
{
    public class ListWithEvents<T> : IList<T>
    {
        private List<T> _list;
        public event EventHandler<ListItemEventArgs<T>> ItemAdded;
        public event EventHandler<ListItemEventArgs<T>> ItemRemoved;
        
        public ListWithEvents()
        {
            _list = new List<T>();
        }

        public void OnItemAdded(ListItemEventArgs<T> e)
        {
            if (ItemAdded != null)
            {
                ItemAdded(this, e);
            }
        }

        public void OnItemRemoved(ListItemEventArgs<T> e)
        {
            if (ItemRemoved != null)
            {
                ItemRemoved(this, e);
            }
        }

        public IList<T1> Filter<T1>() where T1:class
        {
            return (IList<T1>)(from a in _list where a is T1 select a as T1).ToList();
        }

        #region IList<T> Members

        public int IndexOf(T item)
        {
            return _list.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            _list.Insert(index, item);
            OnItemAdded(new ListItemEventArgs<T>(item));
        }

        public void RemoveAt(int index)
        {
            T item = _list[index];
            _list.RemoveAt(index);
            OnItemRemoved(new ListItemEventArgs<T>(item));
        }

        public T this[int index]
        {
            get
            {
                return _list[index];
            }
            set
            {
                _list[index] = value;
            }
        }

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            _list.Add(item);
            OnItemAdded(new ListItemEventArgs<T>(item));
        }

        public void Clear()
        {
            _list.Clear();
        }

        public bool Contains(T item)
        {
            return _list.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _list.Count; }
        }

        bool ICollection<T>.IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public bool Remove(T item)
        {
            if (_list.Remove(item))
            {
                OnItemRemoved(new ListItemEventArgs<T>(item));
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return _list.GetEnumerator();
        }

        #endregion
    }

    public class ListItemEventArgs<T> : EventArgs
    {
        public T Item { get; set; }

        public ListItemEventArgs(T item)
        {
            Item = item;
        }
    }
}
